<style lang="less" scoped>

  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }

  .f-w30 {
    width: 30%;
  }
</style>

<template>
  <section>
    <!--查询工具条-->
    <search-bar>
      <el-row>
        <el-col :span="8">
          <el-form-item label="合伙人姓名">
            <el-select class="f-w30" style="width: 100%" v-model="partnerId"
                       @visible-change="searchPartners"
                       filterable
                       placeholder="请选择合伙人">
              <el-option v-for="partner in partners" :label="partner.realName" :value="partner.id" :key="partner.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="16">
          <el-row>
            <div style="display: inline-block">
              <el-form-item label="统计日期">
                <el-button-group>
                  <el-button style="padding-left: 15px;padding-right: 15px" @click="searchToday">今日</el-button>
                  <el-button style="padding-left: 15px;padding-right: 15px" @click="searchWeek">近一周</el-button>
                  <el-button style="padding-left: 15px;padding-right: 15px" @click="searchMonth">近一月</el-button>
                </el-button-group>
              </el-form-item>
            </div>
            <div style="display: inline-block">
              <el-form-item labelWidth="10px">
                <el-date-picker
                  v-model="searchBarCreateDate3"
                  type="daterange"
                  align="right"
                  placeholder="选择日期范围">
                </el-date-picker>
              </el-form-item>
            </div>
          </el-row>
        </el-col>
      </el-row>
    </search-bar>

    <!-- 操作 -->
    <el-col :span="24" style="margin-bottom: 10px">
      <el-button type="primary" @click="partnerLogTotalExpress" style="float: left">导出</el-button>
      <el-button type="primary" @click="searchResert" style="float: right">重置</el-button>
      <el-button type="primary" @click="searchPartner" style="float: right">查询</el-button>
    </el-col>
    <!--操作end-->

    <!--事业合伙人 列表-->
    <el-table :data="tableData" highlight-current-row border show-summary stripe max-height="650"
              @selection-change="selsChange" v-loading="listLoading" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column prop="name" label="合伙人姓名" width="150" >
      </el-table-column>
      <el-table-column prop="totalAmount" label="总收益(元)" width="150" >
      </el-table-column>
      <el-table-column prop="newAddcustomer" label="新增客户数" width="150" >
      </el-table-column>
      <el-table-column prop="countOrder" label="下单总量(票)" width="180" >
      </el-table-column>
      <el-table-column prop="orderPrice" label="订单总额(元)" width="250" >
      </el-table-column>
      <el-table-column prop="orderWeight" label="重量(T)" width="180" >
      </el-table-column>
      <el-table-column prop="orderVolume" label="体积m³" width="180" >
      </el-table-column>
      <el-table-column label="操作" width="380">
        <template slot-scope="scope">
          <el-button type="primary"  @click="handleRecord(scope.$index, scope.row)">收益记录
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条1-->
    <el-col :span="24" class="toolbar">
      <el-pagination
        :current-page="currentPage"
        :page-size="pageSize"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total,sizes,prev, pager, next, jumper"
        :total="total" style="float: right">
      </el-pagination>
    </el-col>

    <!--收益 记录 弹窗-->
    <el-dialog :title="earningsTitle" size="large" :visible.sync="earningsVisible"
               :close-on-click-modal="false">
      <el-form label-width="100px">
        <!--工具条-->
        <search-bar more-display>
          <el-row>
            <el-col :span="6">
              <el-form-item label="订单编号" :label-width="formLabelWidth">
                <el-input class="f-w70" v-model="earningsOrderNumber" placeholder="请输入订单编号"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="客户名称" :label-width="formLabelWidth">
                <el-select class="f-w70" v-model="MemberUserId"
                           @visible-change="searchMemberUsers"
                           filterable
                           placeholder="请选择客户">
                  <el-option v-for="asd in MemberUsers" :label="asd.realName"
                             :key="asd.userId"
                             :value="asd.userId"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="统计日期" :label-width="formLabelWidth">
                <el-date-picker
                  v-model="earningsDate"
                  type="daterange"
                  align="right"
                  placeholder="选择日期范围">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
        </search-bar>

        <!-- 操作 -->
        <el-col :span="24" style="margin-bottom: 10px">
          <el-button @click="partnerLogExpress" type="primary">导出</el-button>
          <el-button type="primary" @click="earningsResert" style="float: right">重置</el-button>
          <el-button type="primary" @click="searchEarning" style="float: right">查询</el-button>
        </el-col>
        <!--操作end-->
        <!--收益 表-->
        <el-table :data="earningsTableData" highlight-current-row border show-summary stripe
                  v-loading="listLoading2" @sort-change="sortChange2" @selection-change="selsChange2"
                  style="width: 100%;">
          <el-table-column type="selection" width="55">
          </el-table-column>
          <el-table-column prop="orderNumber" label="订单编号" >
          </el-table-column>
          <el-table-column prop="orderCreateDate" label="下单日期" width="150" >
          </el-table-column>
          <el-table-column prop="memberName" label="客户名称" width="150" >
          </el-table-column>
          <el-table-column prop="orderPrice" label="订单金额(元)" width="180" >
          </el-table-column>
          <el-table-column prop="amount" label="收益金额(元)" width="250" >
          </el-table-column>
          <el-table-column prop="orderCargoName" label="货物名称" width="180" >
          </el-table-column>
          <el-table-column prop="quantity" label="件数" width="180" >
          </el-table-column>
          <el-table-column prop="weight" label="重量(T)" width="180" >
          </el-table-column>
          <el-table-column prop="volume" label="体积m³" width="180" >
          </el-table-column>
        </el-table>

        <!--工具条2-->
        <el-col :span="24" class="toolbar">
          <el-pagination
            :current-page="currentPage2"
            :page-size="pageSize2"
            @size-change="handleSizeChange2"
            @current-change="handleCurrentChange2"
            layout="total,sizes,prev, pager, next, jumper"
            :total="total2" style="float: right">
          </el-pagination>
        </el-col>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="earningsVisible = false">取消</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
import {getMemberUsers} from '../../api/customer/maintain.js';
import {
  searchPattners,
  searchPartner,
  searchPattnerPorfitLogDetail,
  searchPattnerPorfitLogDetailNoPage,
  searchPattnersNoPage
} from '../../api/system/partner.js';
export default {
  data () {
    return {
      formLabelWidth: '80px',
      countDayFront: '', // 统计日期   前
      countDayLater: '', // 统计日期   后
      currendDay: 1, // 今 日-7日-近一月的按键
      currendId: '',
      earningsOrderNumber: '',
      earningsTableData: [],
      earningsDate: [],
      MemberUserId: '',
      MemberUsers: [],
      orderNumber: '', // 收益 人记录--订单编号
      partnerId: '', // 会员
      partners: [], // 会员
      searchBarCreateDate3: [],
      earningsVisible: false,

      earningsTitle: '合伙人收益记录',
      condition: {
        name: '',
        number: ''
      },

      // ------------------------------------------------------------
      sels: [],
      sels2: [],
      tableData: [],
      total: 0,
      total2: 0,
      currentPage: 1,
      currentPage2: 1,
      pageSize: 10,
      pageSize2: 10,
      sortProp: null,
      sortOrder: 'ascending',
      sortProp2: null,
      sortOrder2: 'ascending',
      submitLoading: false,
      listLoading: false,
      listLoading2: false,
      countDay: ''
    };
  },
  methods: {
    // 合伙人总的收益表
    partnerLogTotalExpress () {
      searchPattnersNoPage({
        filters: ''
      }).then((res) => {
        if (res.data.successFlg) {
          console.log(res.data.detailModelList);
          this.$ue.downloadFile(res.data.detailModelList.map(item => [item.name, item.totalAmount, item.countOrder, item.orderPrice, item.orderWeight, item.orderVolume]), ['合伙人姓名 ', '总收益(元)', '下单总量(票)', '订单总额(元)', '重量(T)', '体积'], '合伙人总收益导出文件');
        } else {
          this.$message.error('暂无数据，无法导出');
        }
      });
    },

    //  合伙人收益详情导出
    partnerLogExpress () {
      searchPattnerPorfitLogDetailNoPage(this.currendId).then((res) => {
        if (res.data.successFlg) {
          this.$ue.downloadFile(res.data.detailModelList.map(item => [item.orderCreateDate, item.orderNumber, item.memberName, item.orderPrice, item.amount, item.orderCargoName, item.quantity, item.weight, item.volume]), ['下单时间 ', '订单编号', '客户名称', '订单金额(元)', '收益金额(元)', '货物名称', '件数', '重量(T)', '体积'], '合伙人收益导出文件');
        } else {
          return this.$message.error('暂无数据，无法导出');
        }
      });
    },
    searchToday () {
      const end = new Date();
      const start = new Date();
      this.searchBarCreateDate3 = [start, end];
    },
    // 这一周
    searchWeek () {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
      this.searchBarCreateDate3 = [start, end];
    },
    // 这一月
    searchMonth () {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
      this.searchBarCreateDate3 = [start, end];
    },

    // 搜索合伙人收益 详情
    searchEarning () {
      this.listLoading2 = true;
      let filters = '';
      if (this.earningsOrderNumber !== '') {
        filters += 'orderNumber?' + this.earningsOrderNumber + ' g11;';
      }
      if (this.MemberUserId !== '') {
        filters += 'mebId=' + this.MemberUserId + ' g12;';
      }
      if (this.earningsDate.length > 0 && this.earningsDate[0] != null && this.earningsDate[1] != null) {
        filters += 'orderCreateDate>=' + this.earningsDate[0].toJSON() + ';';
        filters += 'orderCreateDate<=' + this.earningsDate[1].toJSON() + ';';
      }
      searchPattnerPorfitLogDetail({
        partnerId: this.currendId,
        page: this.currentPage2,
        size: this.pageSize2,
        filters: filters,
        sorts: (this.sortProp2 === null ? '' : (this.sortOrder2 === 'ascending' ? '-' : '+') + this.sortProp2)
      }).then((res) => {
        this.earningsTableData = [];
        this.listLoading2 = false;
        if (res.data.successFlg) {
          console.log(res.data.detailModelList);
          this.earningsTableData = res.data.detailModelList;
          this.currentPage2 = res.data.currPage;
          this.total2 = res.data.totalCount;
          this.pageSize2 = res.data.pageSize;
        } else {
          this.$message.error(res.data.errorMsg);
        }
      });
    },

    // 重置 合伙人收益 详细
    earningsResert () {
      this.earningsDate = '';
      this.MemberUserId = '';
      this.earningsOrderNumber = '';
    },

    // 搜索 合伙人总
    searchPartner () {
      let filters = '';
      // 合伙人ID
      if (this.partnerId !== '') {
        filters += 'LIKE_id=' + this.partnerId + ';';
      }
      // 统计日期
      if (this.searchBarCreateDate3 !== '' && this.searchBarCreateDate3[0] != null && this.searchBarCreateDate3[0] != null) {
        this.countDayFront = this.searchBarCreateDate3[0].Format('yyyy-MM-dd');
        this.countDayLater = this.searchBarCreateDate3[1].Format('yyyy-MM-dd');
      }
      this.listLoading = true;
      searchPattners({
        countDayFront: this.countDayFront,
        countDayLater: this.countDayLater,
        countDay: this.currendDay,
        page: this.currentPage,
        size: this.pageSize,
        filters: filters,
        sorts: (this.sortProp === null ? '' : (this.sortOrder === 'ascending' ? '-' : '+') + this.sortProp)
      }).then((res) => {
        this.listLoading = false;
        if (res.data.successFlg === true) {
          this.tableData = res.data.detailModelList;
          this.currentPage = res.data.currPage;
          this.total = res.data.totalCount;
          this.pageSize = res.data.pageSize;
        } else {
          this.$message.error(res.data.errorMsg);
        }
      });
    },

    // 搜索栏重置
    searchResert () {
      this.partnerId = '';
      this.searchBarCreateDate3 = '';
      this.currendDay = 1;
      this.countDayFront = '';
      this.countDayLater = '';
    },

    // 收益记录
    handleRecord (index, row) {
      this.currendId = row.partnerId;
      this.listLoading2 = true;
      searchPattnerPorfitLogDetail({
        partnerId: this.currendId,
        page: this.currentPage2,
        size: this.pageSize2,
        filters: '',
        sorts: (this.sortProp2 === null ? '' : (this.sortOrder2 === 'ascending' ? '-' : '+') + this.sortProp2)
      }).then((res) => {
        this.listLoading2 = false;
        this.earningsTableData = [];
        if (res.data.successFlg) {
          this.earningsTableData = res.data.detailModelList;
          this.currentPage2 = res.data.currPage;
          this.total2 = res.data.totalCount;
          this.pageSize2 = res.data.pageSize;
        } else {
          this.$message.error(res.data.errorMsg);
        }
      });

      this.earningsVisible = true;
    },

    // 初始化 搜索合伙人收益
    searchPattnerss () {
      this.listLoading = true;
      this.countDay = 1;
      searchPattners({
        countDayFront: this.countDayFront,
        countDayLater: this.countDayLater,
        countDay: this.countDay,
        page: this.currentPage,
        size: this.pageSize,
        filters: '',
        sorts: (this.sortProp === null ? '' : (this.sortOrder === 'ascending' ? '-' : '+') + this.sortProp)
      }).then((res) => {
        this.listLoading = false;
        if (res.data.successFlg) {
          this.tableData = res.data.detailModelList;
          this.currentPage = res.data.currPage;
          this.total = res.data.totalCount;
          this.pageSize = res.data.pageSize;
        } else {
          this.$message.error(res.data.errorMsg);
        }
      });
    },
    // 多选操作
    selsChange (sels) {
      this.sels = sels;
    },

    // 排序操作
    sortChange (o) {
      this.sortProp = o.prop;
      this.sortOrder = o.order;
      this.searchPartner();
    },

    // 每页几条操作
    handleSizeChange (val) {
      this.pageSize = val;
      this.searchPartner();
    },

    // 第几页操作
    handleCurrentChange (val) {
      this.currentPage = val;
      this.searchPartner();
    },

    // 多选操作
    selsChange2 (sels) {
      this.sels2 = sels;
    },

    // 排序操作
    sortChange2 (o) {
      this.sortProp2 = o.prop;
      this.sortOrder2 = o.order;
      this.searchEarning();
    },

    // 每页几条操作
    handleSizeChange2 (val) {
      this.pageSize2 = val;
      this.searchEarning();
    },

    // 第几页操作
    handleCurrentChange2 (val) {
      this.currentPage2 = val;
      this.searchEarning();
    },
    // 搜索会员名称
    searchPartners () {
      searchPartner({
        token: this.token,
        filters: '',
        sorts: '',
        size: '8',
        page: '1'
      }).then((res) => {
        this.partners = res.data.detailModelList;
      });
    },

    searchMemberUsers () {
      getMemberUsers({
        token: this.token,
        filters: 'status=111002001 g0;partnerId=' + this.currendId + ' g1;',
        sorts: '',
        size: '100',
        page: '1'
      }).then((res) => {
        this.MemberUsers = res.data.detailModelList;
      });
    }

  },
  // 载入合伙人列表

  mounted () {
    this.searchPattnerss();
  }

};
</script>
